package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Admin;
import com.example.mapper.AdminMapper;
import com.example.service.AdminService;
import com.example.utils.Result;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 管理员服务实现类
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    /**
     * 管理员登录
     */
    @Override
    public Result<Admin> login(String username, String password) {
        // 验证参数
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            return Result.error("用户名和密码不能为空");
        }

        // 查询用户
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUsername, username);
        Admin admin = this.getOne(queryWrapper);

        // 验证用户存在
        if (admin == null) {
            return Result.error("用户不存在");
        }

        // 验证密码
        if (!password.equals(admin.getPassword())) {
            return Result.error("密码错误");
        }

        // 登录成功
        return Result.success(admin);
    }
}